package spzx.controller;

import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import spzx.service.SysUserService;

import java.util.List;

@RestController
@RequestMapping("/admin/system/sysUser")
public class SystemUserController {
    @Autowired
    private SysUserService sysUserService;


    /**
     * 根据id批量删除系统用户
     * @param ids
     * @return
     */
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids){
        String message = sysUserService.deleteByIds(ids);
        return Result.build(StringUtils.isEmpty(message)?"已全部删除":message, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除系统用户
     * @param sysUserId
     * @return
     */
    @DeleteMapping("deleteById/{sysUserId}")
    public Result deleteById(@PathVariable Long sysUserId){
        sysUserService.deleteById(sysUserId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }



    /**
     * 添加系统用户
     * @param sysUser
     * @return
     */
    @PostMapping("addSysUser")
    public Result addSysUser(@RequestBody SysUser sysUser){
        sysUserService.addSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加系统用户
     * @param sysUser
     * @return
     */
    @PutMapping("updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 条件+分页查询系统用户列表
     * @param pageNum
     * @param pageSize
     * @param sysUserDto
     * @return 业务数据：PageInfo
     */
    @PostMapping("findByPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@RequestBody SysUserDto sysUserDto){
        PageInfo pageInfo = sysUserService.findByPage(pageNum,pageSize,sysUserDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
